package cn.leonis.leetcode;

import java.util.Arrays;

/**
 * @Author Tobi
 * @Date 2021-12-10 下午4:39
 * 反转字符串
 * leetcode344
 */
public class ReverseStringSolution {
    public static void main(String[] args) {
        char[] s = {'h', 'e', 'l', 'l', 'o'};
        ReverseStringSolution solution = new ReverseStringSolution();
        solution.reverseString(s);
        for (char c: s) {
            System.out.print(c);
        }
    }

    /**
     * 解法：双指针
     * @param s
     */
    public void reverseString(char[] s) {
        int left = 0, right = s.length - 1;
        while (left < right) {
            if (s[left] != s[right]) {
                char c = s[left];
                s[left] = s[right];
                s[right] = c;
            }
            ++left;
            --right;
        }
    }
}
